package com.feihong.yw189.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.yw189.R;

/**
 * @Author wangziheng
 * @ProjectName ywydt
 * @ClassName ShareBottomDialog
 * @package com.feihong.yw189.dialog
 * @data 2018/9/21 14:55
 * @Describe
 * @Change
 */
public class ShareBottomDialog extends Dialog {
    private Context mContext;
    private TextView mTitleTv, mCannelTv, mGoneTv;
    private OperateListner mOperateListner;
    private LinearLayout mBottomContentLayout;
    private RelativeLayout mBottomLayout;
    public ShareBottomDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ShareBottomDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.DialogStyleBottom);
        mContext = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.bottom_in_dialog_layout);
        mBottomLayout = findViewById(R.id.bottom_dialog_layout);
        mTitleTv = findViewById(R.id.dialog_Title_tv);
        mCannelTv = findViewById(R.id.dialog_Content_tv);
        mGoneTv = findViewById(R.id.dialog_cannel_tv);
        mBottomContentLayout = findViewById(R.id.Bottom_title_content_layout);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mBottomContentLayout.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin,layoutParams.topMargin,layoutParams.rightMargin,20);
        mBottomContentLayout.setLayoutParams(layoutParams);
        mGoneTv.setVisibility(View.GONE);
        mTitleTv.setText("保存到本地");
        mCannelTv.setText("取消");
        mBottomLayout.setOnClickListener(onClickListener);
        mTitleTv.setOnClickListener(onClickListener);
        mCannelTv.setOnClickListener(onClickListener);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
    }

    private View.OnClickListener onClickListener = v -> {
        switch (v.getId()) {
            case R.id.dialog_Title_tv:
                mOperateListner.operate();
                break;
            case R.id.dialog_Content_tv:
                cancel();
                break;
            default:
                cancel();
                break;
        }
    };

    public void setOperateListener(OperateListner operateListener) {
        mOperateListner = operateListener;
    }

    public interface OperateListner {
        void operate();
    }

    public void showDialog() {
        show();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        //设置宽度
        lp.width = (display.getWidth());
        getWindow().setAttributes(lp);
    }
}
